import { getCurrentInstance } from 'vue'
import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { delUser, listUser, getUser, addUser, updateUser,deptTreeSelect } from '@/api/system/user.js'

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'userId'
  const moduleName =  '用户'
  const apiFn = {
    listApiFn: listUser,
    delApiFn: delUser,
    apiFnConfig:{
      info: getUser,
      add: addUser,
      edit: updateUser
    }
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  /** 查询部门下拉树结构 */
  const deptOptions = ref([])
  function getDeptTree() {
    deptTreeSelect().then((response) => {
      deptOptions.value = response.data
    })
  }
  getDeptTree()


  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
    deptOptions
  }

}

/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_normal_disable', 'sys_user_sex'
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: '用户编号',
      prop: 'userId',
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: 'center',
      },
    },
    {
      label: '用户名称',
      prop: 'userName',
      tableColumnProps: {
        align: 'center',
      },
      orderInForm:999,
      hideInForm: computed(()=>!!formData.value.userId)
    },
    {
      label: '用户密码',
      prop: 'password',
      hideInTable:true,
      hideInSearch: true,
      orderInForm:988,
      hideInForm: computed(()=>!!formData.value.userId)
    },
    {
      label: '用户昵称',
      prop: 'nickName',
      hideInSearch: true,
      tableColumnProps: {
        align: 'center',
      },
      orderInForm:977,
    },
    {
      label: '部门',
      prop: isForm ? 'deptId': 'deptNames',
      hideInSearch: true,
      tableColumnProps: {
        align: 'center',
      },
      render: (value,{ row }) => row.dept?.deptName,
      orderInForm:966,
    },
    {
      label: '手机号',
      prop: 'phonenumber',
      tableColumnProps: {
        align: 'center',
      },
      orderInForm:955,
    },
    {
      label: '邮箱',
      prop: 'email',
      hideInSearch: true,
      hideInTable: true,
      orderInForm:944,
    },
    {
      label: '性别',
      prop: 'sex',
      valueType: 'select',
      options: dictRawData.sys_user_sex,
      hideInSearch: true,
      hideInTable: true,
      orderInForm:933,
    },
    {
      label: '状态',
      prop: 'status',
      valueType: 'select',
      options: dictRawData.sys_normal_disable,
      tableColumnProps: {
        align: 'center',
      },
      orderInForm:922,
    },
    {
      label: '岗位',
      prop: 'postIds',
      hideInTable: true,
      hideInSearch: true,
      orderInForm:911,
    },
    {
      label: '角色',
      prop: 'roleIds',
      hideInTable: true,
      hideInSearch: true,
      orderInForm:900,
    },
    {
      label: '备注',
      prop: 'remark',
      hideInTable: true,
      hideInSearch: true,
      orderInForm:899,
      colProps: {
        span: 24
      },
      fieldProps: {
        type: 'textarea',
        rows: 2
      }
    },
    {
      label: '创建时间',
      prop: 'createTime',
      valueType: 'date-picker',
      hideInForm: true,
      tableColumnProps: {
        align: 'center',
      },
      fieldProps: {  type: 'daterange', valueFormat: 'YYYY-MM-DD' },
      render: value => proxy.parseTime(value)
    },
    {
      label: '操作',
      prop: 'action',
      width:'170px',
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: 'center',
        fixed:'right'
      },
    }
  ]

  const columns = generateColumns(columnsData,isForm);
  const dictData = computed(()=>generateDictData(dictRawData))

  return {
    dictData,
    columns,
  }
}



